package com.schrodinger.jdbc.connection;

import java.beans.PropertyVetoException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.dom4j.Document;
import org.dom4j.Element;

import com.mchange.v2.c3p0.ComboPooledDataSource;
import com.schrodinger.jdbc.connection.dbcp.DbcpDataSource;
import com.schrodinger.jdbc.connection.xmlutils.JdbcConDom4jUtils;

public class JdbcConnectionUtils {
	
	private static Map<String,ComboPooledDataSource> c3p0DataSourceMap = null;
	private static Map<String,DbcpDataSource> dbcpDataSourceMap = null;
	
	static{
		try {
			loadDataSourcesConfig();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (PropertyVetoException e) {
			e.printStackTrace();
		}
	}
	
	@SuppressWarnings("unchecked")
	private static void loadDataSourcesConfig() throws PropertyVetoException, ClassNotFoundException{
		c3p0DataSourceMap = new HashMap<String,ComboPooledDataSource>();
		dbcpDataSourceMap = new HashMap<String,DbcpDataSource>();
		Document doc = JdbcConDom4jUtils.getDocument("jdbcConfig.xml");
		Element root = doc.getRootElement();
		List<Element> dataSourceList = root.elements();
		for(Iterator<Element> it = dataSourceList.iterator();it.hasNext();){
			Element element = it.next();
			String connectionPool = element.attributeValue("connectionPool");
			String dbname = element.attributeValue("dbname");
			String driverClassName = element.element("driverClassName").getTextTrim();
			String url = element.element("url").getTextTrim();
			String username = element.element("username").getTextTrim();
			String password = element.element("password").getTextTrim();
			if("c3p0".equalsIgnoreCase(connectionPool)){
				ComboPooledDataSource comboPooledDataSource= new ComboPooledDataSource();
				String initialPoolSize = "5";
				if(element.element("initialPoolSize") != null){
					initialPoolSize = element.element("initialPoolSize").getTextTrim();
				}
				String minPoolSize = "1";
				if(element.element("minPoolSize") != null){
					minPoolSize = element.element("minPoolSize").getTextTrim();
				}
				String maxPoolSize = "10";
				if(element.element("maxPoolSize") != null){
					maxPoolSize = element.element("maxPoolSize").getTextTrim();
				}
				String maxStatements = "50";
				if(element.element("maxStatements") != null){
					maxStatements = element.element("maxStatements").getTextTrim();
				}
				String maxIdleTime = "60";
				if(element.element("maxIdleTime") != null){
					maxIdleTime = element.element("maxIdleTime").getTextTrim();
				}
				comboPooledDataSource.setDriverClass(driverClassName);
				comboPooledDataSource.setJdbcUrl(url);
				comboPooledDataSource.setUser(username);
				comboPooledDataSource.setPassword(password);
				comboPooledDataSource.setInitialPoolSize(Integer.parseInt(initialPoolSize));
				comboPooledDataSource.setMinPoolSize(Integer.parseInt(minPoolSize));
				comboPooledDataSource.setMaxPoolSize(Integer.parseInt(maxPoolSize));
				comboPooledDataSource.setMaxStatements(Integer.parseInt(maxStatements));
				comboPooledDataSource.setMaxIdleTime(Integer.parseInt(maxIdleTime));
				c3p0DataSourceMap.put(dbname, comboPooledDataSource);
			}else if("dbcp".equalsIgnoreCase(connectionPool)){
				DbcpDataSource dbcpDataSource = new DbcpDataSource();
				dbcpDataSource.setPoolName(dbname);
				dbcpDataSource.setDriver(driverClassName);
				dbcpDataSource.setUrl(url);
				dbcpDataSource.setName(username);
				dbcpDataSource.setPassword(password);
				String maxActive = "500";
				if(element.element("maxActive") != null){
					maxActive = element.element("maxActive").getTextTrim();
				}
				dbcpDataSource.setMaxActive(Integer.parseInt(maxActive));
				String maxIdle = "10";
				if(element.element("maxIdle") != null){
					maxIdle = element.element("maxIdle").getTextTrim();
				}
				dbcpDataSource.setMaxIdle(Integer.parseInt(maxIdle));
				String maxWait = "2000";
				if(element.element("maxWait") != null){
					maxWait = element.element("maxWait").getTextTrim();
				}
				dbcpDataSource.setMaxWait(Integer.parseInt(maxWait));
				String minIdle = "2000";
				if(element.element("minIdle") != null){
					minIdle = element.element("minIdle").getTextTrim();
				}
				dbcpDataSource.setMinIdle(Integer.parseInt(minIdle));
				dbcpDataSourceMap.put(dbname, dbcpDataSource);
			}
		}
	}
	
	public static Map<String, ComboPooledDataSource> getC3p0DataSourceMap() throws PropertyVetoException, ClassNotFoundException {
		if(c3p0DataSourceMap == null || dbcpDataSourceMap == null){
			loadDataSourcesConfig();
		}
		return c3p0DataSourceMap;
	}
	
	public static Map<String, DbcpDataSource> getDbcpDataSourceMap() throws PropertyVetoException, ClassNotFoundException {
		if(c3p0DataSourceMap == null || dbcpDataSourceMap == null){
			loadDataSourcesConfig();
		}
		return dbcpDataSourceMap;
	}
	
	/**
	 * @param args
	 * @throws PropertyVetoException 
	 * @throws ClassNotFoundException 
	 */
	public static void main(String[] args) throws PropertyVetoException, ClassNotFoundException {
		loadDataSourcesConfig();
	}

}
